Skip to content

Add support for writing struct arrays to excel table #2181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

d9c4
Copy link

@d9c4 d9c4 commented Jul 24, 2025

PR Details

Adds support for exporting slices of tagged structs to Excel in a tabular format.

Description

Add support to quickly write a slice of structs on an Excel table. The feature uses generics and tags.
Fields are read via struct tags and written as column headers. Supports basic types and nested types.

Related Issue

#1546

Motivation and Context

This feature was added to simplify the process of exporting data to Excel. Previously, generating a table required manual formatting for each dataset. Now, struct tags and generics can be used to define headers and structure, reducing repetitive code and improving maintainability.

How Has This Been Tested

Unit tests were added in this commit for the added features.

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Usage example

Code usage

type testStruct struct {
	Column1 string `column:"A" columnHeader:"Column 1"`
	Column2 string `column:"B" columnHeader:"Column 2"`
}
var testStructs = []testStruct{
	{Column1: "1", Column2: "2"},
	{Column1: "3", Column2: "4"},
}
WriteStructsIntoFile(f, testStructs, &ModelTableOptions{HasHeader: true})

Excel output example

Column 1 Column 2
1 2
3 4

Adds support for exporting slices of tagged structs to Excel in a tabular format. Fields are read via struct tags and written as column headers. Supports basic types.
@xuri xuri added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jul 24, 2025
Copy link
Member

@xuri xuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your pull request. I recommend user to using SetSheetRow function instead of this. Users can easily make this wrapper and even create their own complex nested structure. I didn't suggest introducing this feature to make the library core and easy.

Also reference related issues: #619, #992, #1208, #1303, #1357, #1540, #1636 and pull request #1011.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants